/**
 * @author Mabel Zhang
 */

public class Spot
{

  int mmId = 0;
  
  String stName = "";
  String stFrom = "";
  String stTo = "";
  
  float lng = 0;
  float lat = 0;

  // For interface
  boolean isAvailable = false;
  int userId = 0;

  Spot (int mm, String name, String from, String to)
  {
//    System.err.println ("in Spot ctor");
    mmId = mm;
    stName = name;
    stFrom = from;
    stTo = to;
    
    getLongLat ();
  }
  
  void getLongLat ()
  {
    
    String encoded = stName.replace(' ', '+');
    ParseXML xml = new ParseXML (encoded);

//    ParseXML xml = new ParseXML ("7+Ave+and+Charles+Street,+New+York,+NY");
    lng = xml.getLat();
    lat = xml.getLong();
  }
  
  int getMmId ()
  {
    return mmId;
  }
  
  String getStName ()
  {
    return stName;
  }
  
  String getStFrom ()
  {
    return stFrom;
  }
  
  String getStTo ()
  {
    return stTo;
  }
  
  boolean isOpen ()
  {
    return isAvailable;
  }
  
  public void setOpen(boolean open)
  {
    isAvailable = open;
  }
  
  /**
   * Occupy this spot, assign it to user id
   * @param id
   */
  void occupy (int id)
  {
    userId = id;
    isAvailable = false;
  }
  
  /**
   * Free up spot, reset userId to 0
   */
  void free ()
  {
    isAvailable = true;
    userId = 0;
  }

  public void setLng(float lg)
  {
    lng = lg;
  }
  
  public float getLng() 
  {
    return lng;
  }

  public void setLat(float lat) {
    this.lat = lat;
  }

  public float getLat ()
  {
    return lat;
  }
  
  @Override
  public boolean equals(Object obj) {
    
    if (this == obj)
      return true;
    if (obj == null)
      return false;
    if (getClass() != obj.getClass())
      return false;
    
    Spot other = (Spot) obj;
    if (Float.floatToIntBits(lat) != Float.floatToIntBits(other.lat))
      return false;
    if (Float.floatToIntBits(lng) != Float.floatToIntBits(other.lng))
      return false;
    if (mmId != other.getMmId ())
      return false;
    return true;
  }
  
  public String toString()
  {
	  return ("The nearest spot is on " + this.stName + " between " + this.stFrom + "and " + this.stTo);
  }
}
